<template>
      <van-icon
      :name="value?'star':'star-o'"
      :color="value?'#3296fa':'#777'"
      @click="onCollect"
      />
</template>

<script>
import { addCollectArticle, deleteCollectArticle } from '@/api/article.js'
import { Toast } from 'vant'
export default {
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Number, Object, String],
      required: true
    }
  },
  data () {
    return {

    }
  },
  model: {
    event: 'onCollectArticle'
  },
  created () {

  },

  methods: {
    async  onCollect () {
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '加载中...'
      })
      try {
        if (this.value) {
        // 点击收藏  实现取消
          await deleteCollectArticle(this.artId + '')
          // this.value = false
        } else {
        // 点击取消  实现收藏
          await addCollectArticle(this.artId + '')
        }
        // this.value = !this.value
        this.$emit('onCollectArticle', !this.value)
        Toast.success('收藏成功')
      } catch (error) {
        Toast.fail('收藏失败')
      }
    }

  }
}
</script>

<style scoped lang='less'>

</style>
